Rem
Dialog GUI helper module for mscchat

Copyright (c) 2009 Michael Schaefer and Immo-Gert Birn

Mscchat is free software: you can redistribute it And/Or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, Or
(at your option) any later version.

Mscchat is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE.  See the
GNU General Public License For more details.

You should have received a copy of the GNU General Public License
along with mscchat.  If Not, see <http://www.gnu.org/licenses/>.
EndRem

Function request_string:String(caption:String, text:String = Null)
  Local ww:Int = 220
  Local wh:Int = 30
  Local wx:Int = Desktop().width/2-ww/2
  Local wy:Int = Desktop().height/2-wh/2
  Local win:TGadget = CreateWindow(caption,wx,wy,ww,wh,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CLIENTCOORDS)
  Local txt:TGadget = CreateTextField(5,5,ww-100,20,win)
  SetGadgetText (txt,text)
  Local ok:TGadget=CreateButton("OK",ww-85,5,80,20,win, BUTTON_OK)
  ActivateGadget(txt)
  Repeat
    Select WaitEvent()
      Case EVENT_WINDOWCLOSE
        FreeGadget(win)
        Return text
      Case EVENT_GADGETACTION
          If EventSource()=ok
          text=GadgetText(txt)
          FreeGadget(win)
		  Return text
        End If
    End Select
  Forever
End Function

Function popup_to_confirm:Int(caption:String, text:String)
  Local ww:Int = 300
  Local wh:Int = 100
  Local wx:Int = Desktop().width/2-ww/2
  Local wy:Int = Desktop().height/2-wh/2
  Local win:TGadget    = CreateWindow(caption,wx,wy,ww,wh,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CLIENTCOORDS)
  Local txt:TGadget    = CreateLabel(text,5,5,ww-5,wh-30,win)
  Local ok:TGadget     = CreateButton("Ja"  , 5,    wh - 20, 80, 20, win, BUTTON_OK)
  Local cancel:TGadget = CreateButton("Nein", ww-85,wh - 20, 80, 20, win)
  Repeat
    Select WaitEvent()
      Case EVENT_WINDOWCLOSE
        FreeGadget(win)
        Return False
      Case EVENT_GADGETACTION
        Local res:Int
        If EventSource() = ok     Then res = True
        If EventSource() = cancel Then res = False
        FreeGadget(win)
        Return res
    End Select
  Forever
End Function


Function splash_screen(bild:Object, dauer:Int=4)
  Local Pixmap_Splash:TPixmap
  Local Window_Splash:TGadget
	
  Local Splash_X:Int
  Local Splash_Y:Int
	
  Local Panel_Splash:TGadget
  Local Timer:TTimer
	
  Pixmap_Splash = LoadPixmap(bild)
	
  Splash_X = ClientWidth(Desktop())*0.5
  Splash_X:-PixmapWidth(Pixmap_Splash)*0.5
	
  Splash_Y = ClientHeight(Desktop())*0.5
  Splash_Y:-PixmapHeight(Pixmap_Splash)*0.5
	
  Window_Splash = CreateWindow("",Splash_X, Splash_Y,PixmapWidth(Pixmap_Splash), PixmapHeight(Pixmap_Splash) ,Null, 0)
  'SetGadgetAlpha(window_splash,0)
  Panel_Splash = CreatePanel(0,0,ClientWidth(Window_Splash), ClientHeight(Window_Splash), Window_Splash,PANEL_ACTIVE)
  'SetGadgetAlpha(panel_splash, 0)
  setImageMask(pixmap_splash, 255, 255, 255)
  SetPanelPixmap(Panel_Splash, Pixmap_Splash)
	
  Timer = CreateTimer(1)
	
  Repeat 
    Select PollEvent()
      Case EVENT_TIMERTICK 
        If TimerTicks(Timer) => dauer
          Exit
        EndIf
      Case EVENT_MOUSEDOWN
        Exit
    End Select 
  Forever
	
  StopTimer(Timer)
  FreeGadget(Window_Splash)
  Pixmap_Splash = Null
End Function


Function SetImageMask(p_image:TPixmap, p_red:Int,p_green:Int,p_blue:Int)
	Local l_maskrgb:Int
	Local l_maskargb:Int
	Local l_pixelrgb:Int
	Local l_pixelraw:Int
	Local l_x:Int,l_y:Int
	
	l_maskrgb = p_red Shl 16 + p_green Shl 8 + p_blue
	l_pix1 = p_image 'LockImage(p_image)
	For l_x = 0 To PixmapWidth(p_image)-1
		For l_y = 0 To PixmapHeight(p_image)-1
			l_pixelraw = ReadPixel(l_pix1,l_x,l_y)
			l_pixelrgb = l_pixelraw & 16777215
			If l_pixelrgb = l_maskrgb Then
				WritePixel(l_pix1,l_x,l_y,l_maskrgb)
			End If
		Next 
	Next
	'UnlockImage(p_image)
End Function


' Beispiel: siehe GuiChatClient
Type TIgbProgBar
  Field _progbar:TGadget
  Field _win:TGadget
  Field _timer:TTimer

  Method Create:TIgbProgBar(caption:String)
    Local ww:Int = 400
    Local wh:Int = 50
    Local wx:Int = Desktop().width/2-ww/2
    Local wy:Int = Desktop().height/2-wh/2
    _win     = CreateWindow(caption,wx,wy,ww,wh,Null,WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CLIENTCOORDS)
    _progbar = CreateProgBar(5,5,ww-10,40,_win)
    _timer = CreateTimer(1000)
    ActivateGadget(_win)
    Return Self
  EndMethod
  
  Method update(wert:Int, maxi:Int)
    Local val:Float = Float(wert) / Float(maxi)
    UpdateProgBar(_progbar, val)
  EndMethod

  Method close()
    StopTimer(_timer)
    FreeGadget(_win)
  EndMethod 
EndType
